home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / doc.lha / doc.doc / intro.doc < prev    next >
Text File  |  1992-09-25  |  37KB  |  1,335 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. ___________________________________________________________________
  12.  
  13.  
  14.                                    Toolbox Introduction
  15.  
  16.  
  17.                                    J. Grosch
  18.  
  19.  
  20. ___________________________________________________________________
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. ___________________________________________________________________
  51.                                    GESELLSCHAFT FUeR MATHEMATIK
  52.                                    UND DATENVERARBEITUNG MBH
  53.  
  54.                                    FORSCHUNGSSTELLE FUeR
  55.                                    PROGRAMMSTRUKTUREN
  56.                                    AN DER UNIVERSITAeT KARLSRUHE
  57. ___________________________________________________________________
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.                                    Project
  76.  
  77.                              Compiler Generation
  78.  
  79.          ___________________________________________________________
  80.  
  81.                              Toolbox Introduction
  82.  
  83.  
  84.                                  Josef Grosch
  85.  
  86.  
  87.                                  Aug. 3, 1992
  88.  
  89.          ___________________________________________________________
  90.  
  91.  
  92.                                 Report No. 25
  93.  
  94.  
  95.                              Copyright c 1992 GMD
  96.  
  97.  
  98.             Gesellschaft fuer Mathematik und Datenverarbeitung mbH
  99.                 Forschungsstelle an der Universitaet Karlsruhe
  100.                           Vincenz-Priesznitz-Str. 1
  101.                                D-7500 Karlsruhe
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                                                                              1
  135.  
  136.  
  137.                              Toolbox Introduction
  138.  
  139. Abstract
  140.  
  141. This document introduces into the usage of the Karlsruhe Toolbox for  Compiler
  142. Construction.   It  should  be  read  by those who effectively want to use the
  143. toolbox as first document.  Those who want to learn about the toolbox and  its
  144. contents  in general are referred to the document "A Toolbox for Compiler Con-
  145. struction".
  146.  
  147. This document gives an overview about the documentation  of  the  toolbox.  It
  148. describes  how  the  individual tools interact in order to generate a complete
  149. compiler. The general structure of a makefile to control  the  tools  is  dis-
  150. cussed.
  151.  
  152. 1.  Document Overview
  153.  
  154. The documentation of the Karlsruhe Toolbox for Compiler Construction  consists
  155. of  separate  user's  manuals  for  the individual tools and additional papers
  156.  
  157.                             Table 1: Document Set
  158.  
  159. ________________________________________________________________________________________________
  160.  Filename    Title                                                                        Pages
  161. ________________________________________________________________________________________________
  162.  intro       Toolbox Introduction                                                           12
  163.  toolbox     A Tool Box for Compiler Construction                                           11
  164.  werkzeuge   Werkzeuge fuer den Uebersetzerbau                                              12
  165.  reuse       Reusable Software - A Collection of Modula-2-Modules                           24
  166.  reuseC      Reusable Software - A Collection of C-Modules                                  12
  167.  prepro      Preprocessors                                                                  14
  168.  rex         Rex - A Scanner Generator                                                      32
  169.  scanex      Selected Examples of Scanner Specifications                                    21
  170.  scangen     Efficient Generation of Table-Driven Scanners                                  15
  171.  lalr-ell    The Parser Generators Lalr and Ell                                             43
  172.  lalr        Lalr - A Generator for Efficient Parsers                                       22
  173.  ell         Efficient and Comfortable Error Recovery in Recursive Descent Parsers          15
  174.  highspeed   Generators for High-Speed Front-Ends                                           12
  175.  autogen     Automatische Generierung effizienter Compiler                                  10
  176.  ast         Ast - A Generator for Abstract Syntax Trees                                    36
  177.  toolsupp    Tool Support for Data Structures                                               11
  178.  ag          Ag - An Attribute Evaluator Generator                                          27
  179.  ooags       Object-Oriented Attribute Grammars                                             10
  180.  multiple    Multiple Inheritance in Object-Oriented Attribute Grammars                     10
  181.  puma        Puma - A Generator for the Transformation of Attributed Trees                  29
  182.  trafo       Transformation of Attributed Trees Using Pattern Matching                      16
  183.  minilax     Specification of a MiniLAX-Interpreter                                         35
  184.  begmanual   BEG - a Back End Generator - User Manual                                       71
  185.  mtc         Entwurf und Implementierung eines Uebersetzers von Modula-2 nach C            105
  186.  estra       Spezifikation und Implementierung der Transformation attributierter Baeume     79
  187. ________________________________________________________________________________________________
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.           
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.                                                                                        
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                                                                                                
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.                                                                              2
  307.  
  308.  
  309. describing further aspects  such  as  implementation  details,  examples,  and
  310. applications.   The  documents are written in English. For a few of them there
  311. are German versions as well.  Only the two master thesis about estra  and  mtc
  312. exist in German, only.
  313.  
  314. 1.1.  Format
  315.  
  316. The documents exist in two formats:  Postscript  and  troff.  These  are  dis-
  317. tinguished  by  the file suffixes .ps and .me. The troff files need processing
  318. with pic and the device independent version of troff called ditroff using  me-
  319. macros by commands like
  320.  
  321.     pic | tbl | eqn | ditroff -me
  322.  
  323. Depending on the format the documents are located in the directories doc.ps or
  324. doc.me.
  325.  
  326. 1.2.  Documents
  327.  
  328. Table 1 lists  the  titles  of  the  documents,  the  corresponding  filenames
  329. (without suffix), and the number of pages.
  330.  
  331. 1.3.  Outlines
  332.  
  333. In the following the contents of every document is outlined shortly:
  334.  
  335. Toolbox Introduction
  336.      An introduction for effective users of the toolbox which should  be  con-
  337.      sulted  first.  It gives an overview about the document set and describes
  338.      how the tools interact.
  339.  
  340. A Tool Box for Compiler Construction
  341.      Explains the contents of the toolbox and the underlying design. The indi-
  342.      vidual  tools  are  sketched shortly and some application experiences are
  343.      reported.
  344.  
  345. Werkzeuge fuer den Uebersetzerbau
  346.      A German version of the previous document.
  347.  
  348. Reusable Software - A Collection of Modula-2-Modules
  349.      Describes a library of general routines written  in  Modula-2  which  are
  350.      oriented  towards  compiler construction. The output of some tools has to
  351.      be linked with this library.
  352.  
  353. Reusable Software - A Collection of C-Modules
  354.      Describes a library of general routines written in C which  are  oriented
  355.      towards  compiler construction. The output of some tools has to be linked
  356.      with this library.
  357.  
  358. Preprocessors
  359.      Describes several preprocessors for the extraction of scanner  specifica-
  360.      tions  out  of  parser  specifications and for the conversion of lex/yacc
  361.      input to rex/lalr input or vice versa. There are seven preprocessors:
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.                                                                              3
  373.  
  374.  
  375.          cg -xz  converts an attribute grammar to lalr input
  376.          rpp     combines a grammar and a scanner specification to rex input
  377.          l2r     converts lex input to rex input
  378.          y2l     converts yacc input to lalr input
  379.          r2l     converts rex input to lex input
  380.          cg -u   converts an attribute grammar to yacc input
  381.          bnf     converts a grammar from EBNF to BNF
  382.  
  383.  
  384. Rex - A Scanner Generator
  385.      The user's manual for the scanner generator rex.
  386.  
  387. Selected Examples of Scanner Specifications
  388.      A collection of scanner specifications for rex dealing mostly with patho-
  389.      logical cases.
  390.  
  391. Efficient Generation of Table-Driven Scanners
  392.      Describes internals of the scanner generator rex like the so-called  tun-
  393.      nel  automaton and the linear time algorithm for constant regular expres-
  394.      sions.
  395.  
  396. The Parser Generators Lalr and Ell
  397.      The user's manual for the LALR(1) parser generator  lalr  and  the  LL(1)
  398.      parser  generator ell. It describes among other things the input language
  399.      common to both generators.
  400.  
  401. Lalr - A Generator for Efficient Parsers
  402.      Describes details of the implementation of the parsers generated by  lalr
  403.      and further outstanding features of this tool.
  404.  
  405. Efficient and Comfortable Error Recovery in Recursive Descent Parsers
  406.      Describes the implementation of the parsers generated by  ell  especially
  407.      with respect to automatic error recovery.
  408.  
  409. Generators for High-Speed Front-Ends
  410.      A summary of the highlights of the scanner and parser  generators  and  a
  411.      comparison to lex/yacc and flex/bison.
  412.  
  413. Automatische Generierung effizienter Compiler
  414.      A German version of the previous document.
  415.  
  416. Ast - A Generator for Abstract Syntax Trees
  417.      The user's manual of ast, a tool supporting the definition and  manipula-
  418.      tion of attributed trees and graphs.
  419.  
  420. Tool Support for Data Structures
  421.      Also describes ast, but less precise than the previous document.
  422.  
  423. Ag - An Attribute Evaluator Generator
  424.      The user's manual of ag, a generator for evaluators of ordered  attribute
  425.      grammars (OAG).
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.                                                                              4
  438.  
  439.  
  440. Object-Oriented Attribute Grammars
  441.      Also describes ag, like the  previous  document,  with  emphasis  on  the
  442.      object-oriented features.
  443.  
  444. Multiple Inheritance in Object-Oriented Attribute Grammars
  445.      Extends the object-oriented attribute grammars described in the  previous
  446.      document to multiple inheritance.
  447.  
  448. Spezifikation und Implementierung der Transformation attributierter Baeume
  449.      Diploma thesis in German about the design and implementation of estra,  a
  450.      generator for the transformation of attributed trees.
  451.  
  452. Puma - A Generator for the Transformation of Attributed Trees
  453.      The user's manual of puma, a tool for the  transformation  of  attributed
  454.      trees which is based on pattern matching and unification.
  455.  
  456. Transformation of Attributed Trees Using Pattern Matching
  457.      Also describes puma using a more introductory style and  compares  it  to
  458.      similar tools.
  459.  
  460. Specification of a MiniLAX-Interpreter
  461.      The annotated input to generate a compiler for the example language Mini-
  462.      LAX.
  463.  
  464. BEG - a Back End Generator - User Manual
  465.      The user's manual of the back-end-generator beg.
  466.  
  467. Entwurf und Implementierung eines Uebersetzers von Modula-2 nach C
  468.      Diploma thesis in German about  the  design  and  implementation  of  the
  469.      Modula-to-C translator mtc.
  470.  
  471. For readers intending to use the tools the following documents are of  primary
  472. interest:
  473.  
  474.     intro       Toolbox Introduction
  475.     toolbox     A Tool Box for Compiler Construction
  476.     prepro      Preprocessors
  477.     rex         Rex - A Scanner Generator
  478.     lalr-ell    The Parser Generators Lalr and Ell
  479.     ast         Ast - A Generator for Abstract Syntax Trees
  480.     ag          Ag - An Attribute Evaluator Generator
  481.     puma        Puma - A Generator for the Transformation of Attributed Trees
  482.     begmanual   BEG - a Back End Generator - User Manual
  483.  
  484. Of secondary interest might be:
  485.  
  486.     reuse       Reusable Software - A Collection of Modula-2-Modules
  487.     scanex      Selected Examples of Scanner Specifications
  488.  
  489.                           Fig. 1: Compiler Structure
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.                                                                              5
  503.  
  504.  
  505. The other documents either describe internals of the tools or are excerpts  of
  506. the above.
  507.  
  508. 2.  Generating a Compiler
  509.  
  510. A compiler usually consists of several modules where every  module  handles  a
  511. certain  task.   The  toolbox gives very much freedom for the design of a com-
  512. piler and supports various structures.
  513.  
  514.      Figure 1 presents our preferred compiler structure. In the  right  column
  515. are  the main modules that constitute a compiler. The left column contains the
  516. necessary specifications. In between there are the tools which are  controlled
  517. by  the specifications and which produce the modules. The arrows represent the
  518. data flow in part during generation time and in part during run  time  of  the
  519. compiler.
  520.  
  521.      In principle the compiler model works as follows: A scanner and a  parser
  522. read  the  source, check the concrete syntax, and construct an abstract syntax
  523. tree. They may perform several normalizations, simplifications, or transforma-
  524. tions  in  order  to  keep  the  abstract  syntax  relatively simple. Semantic
  525. analysis is performed on the abstract syntax tree. Optionally  attributes  for
  526. code  generation  may  be  computed.  Afterwards  the  abstract syntax tree is
  527. transformed into an intermediate representation. The latter is  the  input  of
  528. the code generator which finally produces the machine code.
  529.  
  530.      The picture in Figure 1 is relatively abstract by just listing  the  main
  531. tasks  of  a  compiler.   Every  task is generated by a tool out of a separate
  532. specification which is oriented towards the problem at  hand.  The  generation
  533. processes seem to be independent of each other.
  534.  
  535.      For a real user a more closer look than the one of Figure 1 is necessary.
  536. Figure  2  describes the actual interaction among the tools.  It describes the
  537. data flow starting from specifications and ending in an  executable  compiler.
  538. Boxes represent files, circles represent tools, and arrows show the data flow.
  539. The input specifications are contained in the files  at  the  left-hand  side.
  540. The  tools  generate  modules  containing  source  code  in the implementation
  541. languages C or Modula-2. This modules are shown at the right-hand side.  Every
  542. module conists of two files with the following suffixes:
  543.  
  544. implementation language C:
  545.      .h      header or interface file
  546.      .c      implementation part
  547.  
  548. implementation language Modula-2:
  549.      .md     definition module
  550.      .mi     implementation module
  551.  
  552.  
  553.  
  554.  
  555.               Fig. 2: Interaction and data flow among the tools
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.                                                                              6
  569.  
  570.  
  571.      Files outside the left- and right-hand side columns contain  intermediate
  572. data.  The various kinds of information in the files are distinguished by dif-
  573. ferent file types as explained in Table 2.  The few dependencies between tools
  574. are  shown  by  the  data  flow via intermediate files. These dependencies are
  575. explained in more detail in the next sections.
  576.  
  577.                              Table 2: File Types
  578.  
  579.     ______________________________________________________________________
  580.      Suffix   Meaning
  581.     ______________________________________________________________________
  582.      .pars    scanner and parser specification (including S-attribution)
  583.      .scan    rest of scanner specification
  584.      .rpp     intermediate data: scanner description extracted from .pars
  585.      .rex     scanner specification understood by rex
  586.      .lalr    parser specification understood by lalr
  587.      .ell     input for ell (= input for lalr with EBNF constructs)
  588.      .cg      input for ast and ag
  589.      .puma    input for puma
  590.      .ST      intermediate data: storage assignment for attributes
  591.      .TS      intermediate data: description of attributed tree
  592.     ______________________________________________________________________
  593.      .h       C source: header or interface file
  594.      .c       C source: implementation part
  595.      .md      Modula-2 source: definition module
  596.      .mi      Modula-2 source: implementation module
  597.      .exe     compiled and linked executable compiler
  598.     ______________________________________________________________________
  599.    
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.            
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.                                                                          
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651. 2.1.  Scanning and Parsing
  652.  
  653. Two parser generators are contained in the toolbox. First,  the  user  has  to
  654. decide which one to use. I will not start arguing here in favour of one or the
  655. other grammar class.  If I am asked, I recommend to use lalr. Both parser gen-
  656. erators  and their common input language (types .lalr and .ell) are documented
  657. in "The Parser Generators Lalr and Ell".  From the  syntactic  point  of  view
  658. both tools understand almost the same input language. The only incompatibility
  659. concerns the different notation to access attributes. From the semantic  point
  660. of  view there are of course differences with respect to the grammar class and
  661. the kind of attribution evaluable during parsing. Whereas lalr accepts LALR(1)
  662. grammars  and  is  able to evaluate and S-attribution (SAG), ell accepts LL(1)
  663. grammars and is able to evaluate an L-attribution (LAG). Both,  lalr  and  ell
  664. generate  a  module  with the default name Parser which serves as basename for
  665. the file name, too. This module name can be chosen freely using an appropriate
  666. directive  in  the input of the parser generators.  Both parser generators can
  667. also supply a module called Errors. This is a simple  prototype  for  handling
  668. error  messages  that just prints the messages.  However, it is recommended to
  669. use the more comfortable module Errors from  the  library  reuse.   In  simple
  670. cases,  this module is just linked to the user's program. If modifications are
  671. necessary this module should be copied from the library along with its compan-
  672. ion module Positions into the user's directory. The module Positions defines a
  673. data structure to describe source positions.
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.                                                                              7
  685.  
  686.  
  687.      The scanner generator rex and its input language (type  .rex)  are  docu-
  688. mented  in  "Rex  -  A  Scanner  Generator".   rex generates a module with the
  689. default name Scanner which serves as basename for the file name, too.  rex can
  690. also  generate  a  module  called  Source which isolates the task of providing
  691. input for the scanner.  By default it reads  from  a  file  or  from  standard
  692. input.   Again,  it  is  recommended to use the module Source from the library
  693. reuse.  In simple cases, this module is just linked to the user's program.  If
  694. modifications  are  necessary or the module should provide input for a scanner
  695. with a name different to Scanner then this module must be requested from rex.
  696.  
  697.      It is possible to combine several scanners and parsers  either  generated
  698. by lalr or ell into one program as long as different module names are chosen.
  699.  
  700.      If the parser generator ell is to be used, the inputs of rex and ell have
  701. to  be  specified  in  the languages directly understood by these tools (types
  702. .rex and .ell).  If the parser generator lalr is to be used, a  more  comfort-
  703. able  kind of input language is available. It is possible to extract most of a
  704. scanner specification out of a parser grammar.  Therefore it is recommended to
  705. specify  scanner  and  parser  by  two files of types .scan and .pars. Further
  706. advantageous of this approach are that concrete syntax, abstract  syntax,  and
  707. attribute grammar are written in one common language (types .pars and .cg) and
  708. that the attribution to be evaluated during parsing  is  written  using  named
  709. attributes.  This attribution is checked for completeness and whether it obeys
  710. the SAG property.  The language to describe concrete and  abstract  syntax  is
  711. documented in: "Ast - A Generator for Abstract Syntax Trees".  The addition of
  712. attribute declarations and attribute computations are documented in: "Ag -  An
  713. Attribute  Evaluator Generator".  The use of this language especially as input
  714. for scanner and parser generation is  documented  in:  "Preprocessors".   This
  715. document  also  describes  the  preprocessors  cg -xz and rpp. cg -xz converts
  716. input of type .pars into input directly understood by lalr (type .lalr) and it
  717. extracts  most of the scanner specification which is written on the intermedi-
  718. ate file named Scanner.rpp. The rex preprocessor  rpp  merges  this  extracted
  719. scanner  specification with additional parts provided by the user (type .scan)
  720. and produces input directly understood by rex. The language in files  of  type
  721. .scan  is an extension of the input language of rex. These extensions are also
  722. documented in: "Preprocessors".
  723.  
  724. 2.2.  Semantic Analysis and Transformation
  725.  
  726. Our preferred compiler design constructs an abstract syntax tree as underlying
  727. data  structure  for semantic analysis. Afterwards this tree is usually mapped
  728. to some kind of intermediate language by a phase termed transformation.
  729.  
  730.      The syntax tree as central data structure is managed by the module  Tree.
  731. This  module  is generated by the tool ast out of a specification in a file of
  732. type .cg.  The tool ast and its input language are documented  in:  "Ast  -  A
  733. Generator  for  Abstract  Syntax Trees".  The construction of trees is usually
  734. done during parsing. It is specified within the semantic actions of the  input
  735. of the parser generator.
  736.  
  737.      One possibility for the specification of semantic analysis is the use  of
  738. an  attribute  grammar.  The tool ag generates an evaluator module (named Eval
  739. by default) out of an attribute grammar. As this tool also  has  to  know  the
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.                                                                              8
  751.  
  752.  
  753.  
  754.               Table 3: Library Units Needed by Generated Modules
  755.  
  756. ___________________________________________________________________________________
  757.  Tool    Module    C                          Modula-2
  758. ___________________________________________________________________________________
  759.  rex     Source    System                     System
  760.  rex     Scanner   System, Source             System, Checks, Memory, Strings, IO,
  761.                                               Position, Source
  762.  lalr    Parser    Memory, DynArray, Sets,    System, DynArray, Sets, Strings
  763.                    Errors                     Positions, Errors
  764.  lalr    Errors    System, Sets, Idents,      System, Strings, Idents, Sets, IO,
  765.                    Positions                  Positions
  766.  ell     Parser    Errors                     System, Strings, Positions, Errors
  767.  ell     Errors    System, Sets, Idents,      System, Strings, Idents, Sets, IO,
  768.                    Positions                  Positions
  769.  reuse   Errors    System, Memory, Sets,      System, Memory, Strings, StringMem,
  770.                    Idents, Positions          Idents, Sets, IO, Positions, Sort
  771.  ast     Tree      System, General, Memory,   System, General, Memory, DynArray,
  772.                    DynArray, StringMem,       IO, Layout, StringMem, Strings,
  773.                    Idents, Sets, Positions    Idents, Texts, Sets, Positions
  774.  ag      Eval      -                          -
  775.  puma    Trafo     System                     System, IO
  776. ___________________________________________________________________________________
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.       
  796.  
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.                 
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.                                            
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.                                                                                   
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870. structure of the abstract syntax tree both, ast and  ag  usually  process  the
  871. same  input file.  The tool ag and the extensions to the input language of ast
  872. for attribute grammars are documented in: "Ag - An Attribute Evaluator Genera-
  873. tor".
  874.  
  875.      The optimizer of ag decides how to  implement  attributes.  They  can  be
  876. either stored in the tree or in global stacks or global variables. This infor-
  877. mation is communicated from ag to ast in files of type .ST. (This  feature  is
  878. not implemented yet.)
  879.  
  880.      The tool puma generates transformers (named Trafo by  default)  that  map
  881. attributed  trees to arbitrary output. As this tool also has to know about the
  882. structure of the tree this information is communicated from ast to puma via  a
  883. file  of  type  .TS.  The  tool puma and its input language are documented in:
  884. "Puma - A Generator for the Transformation of Attributed Trees".
  885.  
  886.      The names of the modules produced by the tools ast, ag, and puma  can  be
  887. controlled  by  directives  in  the input. Figure 2 uses the default names. By
  888. chosing different names it is possible to combine several tree modules, attri-
  889. bute evaluators, and transformers in one program.
  890.  
  891. 2.3.  Compiling and Linking
  892.  
  893. All the source modules generated by the tools have to be compiled  by  a  com-
  894. piler  appropriate for the implementation language (C or Modula-2). Additional
  895. hand-written modules can be added as necessary. In Figure 2 the module Support
  896. indicates  this  possibility.  In the last step all binaries have to be linked
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.                                                                              9
  908.  
  909.  
  910.  
  911.                     Table 4: Modules in the Library Reuse
  912.  
  913. ____________________________________________________________________________________
  914.  Module      Task                                                      C   Modula-2
  915. ____________________________________________________________________________________
  916.  Memory      dynamic storage (heap) with free lists                    y      y
  917.  Heap        dynamic storage (heap) without free lists                 -      y
  918.  DynArray    dynamic and flexible arrays                               y      y
  919.  Strings     string handling                                           -      y
  920.  StringMem   string memory                                             y      y
  921.  Idents      identifier table - unambiguous encoding of strings        y      y
  922.  Lists       lists of arbitrary objects                                -      y
  923.  Texts       texts are lists of strings (lines)                        -      y
  924.  Sets        sets of scalar values (without run time checks)           y      y
  925.  SetsC       sets of scalar values (with run time checks)              -      y
  926.  Relations   binary relations between scalar values                    -      y
  927.  IO          buffered input and output                                 -      y
  928.  StdIO       buffered IO for standard files                            -      y
  929.  Layout      more routines for input and output                        -      y
  930.  Positions   handling of source positions                              y      y
  931.  Errors      error handler for parsers and compilers                   y      y
  932.  Source      provides input for scanners                               y      y
  933.  Sort        quicksort for arrays with elements of arbitrary type      -      y
  934.  System      interface to the operating system                         y      y
  935.  General     miscellaneous functions                                   y      y
  936. ____________________________________________________________________________________
  937.  
  938.  
  939.  
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.           
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.                                                                     
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.                                                                         
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.                                                                                    
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045. together with a few modules of the library reuse to yield an  executable  com-
  1046. piler.
  1047.  
  1048.      The use of modules from the library reuse depends on  the  implementation
  1049. language  and  the  used  tools.  There  is a C and a Modula-2 version of this
  1050. library.  The Modula-2 version is documented in: "Reusable Software -  A  Col-
  1051. lection  of  Modula-2-Modules".   The  C  version  is documented in: "Reusable
  1052. Software - A Collection of C-Modules".  Table 3 lists the library units needed
  1053. by  tool  generated modules.  Additionally the user may engage further modules
  1054. from this library for various tasks.  Table 4 lists the modules that might  be
  1055. of  interest.  The  right-hand  side  columns describe the availability of the
  1056. modules with regard to the implementation language.
  1057.  
  1058. 3.  Makefile
  1059.  
  1060. The tools of the toolbox are conveniently controlled by the UNIX program  make
  1061. and  an  appropriate makefile - at least under the UNIX operating system. This
  1062. eases the invocation of the tools and minimizes  the  amount  of  regeneration
  1063. after  changes. Figure 2 can be used to derive a makefile because it describes
  1064. most of the dependencies among tools and files. The following  makefiles  con-
  1065. trol  the generation of compilers for the example language MiniLAX in the tar-
  1066. get languages C and Modula-2.  The annotated specification of this language is
  1067. documented  in:  "Specification  of a MiniLAX-Interpreter".  The makefiles are
  1068. examples a user can start with.
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.                                                                             10
  1080.  
  1081.  
  1082.      The makefiles in the next sections deviate in the following from the fun-
  1083. damental structure presented in Figure 2:
  1084.  
  1085. -    The attribute evaluator module is called Semantics instead of Eval.
  1086.  
  1087. -    The transformation module is called ICode instead of Trafo.
  1088.  
  1089. -    The hand-written modules are called ICodeInter and minilax.   The  latter
  1090.      constitutes the main program.
  1091.  
  1092. -    There are two support modules for semantic  analysis  called  Definitions
  1093.      and Types.  These are generated by tools (ast/cg and puma), too.
  1094.  
  1095. 3.1.  C
  1096.  
  1097. The macro LIB specifies the directory where  the  compiled  library  reuse  is
  1098. located.  The  name  of  this library is libreuse.a.  The -I flag in the macro
  1099. CFLAGS specifies the directory where the header files of the  library  modules
  1100. are located.
  1101.  
  1102.      The first command (target minilax) is for linking the compiled modules to
  1103. an  executable compiler. The succeeding entries describe the dependencies dur-
  1104. ing the generation phase and the invocation of the  tools.   The  dependencies
  1105. before the target lint are those needed during compilation.
  1106.  
  1107. LIB     = $(HOME)/lib
  1108. INCDIR  = $(LIB)/include
  1109. CFLAGS  = -I$(INCDIR)
  1110. CC      = cc
  1111.  
  1112. SOURCES = Scanner.h Scanner.c Parser.h Parser.c Tree.h Tree.c \
  1113.           Semantics.h Semantics.c Types.h Types.c Definitions.h Definitions.c \
  1114.           ICode.h ICode.c ICodeInter.h ICodeInter.c minilax.c
  1115.  
  1116. BINS    = minilax.o Scanner.o Parser.o Tree.o \
  1117.           Types.o Definitions.o Semantics.o ICode.o ICodeInter.o
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.                                                                             11
  1145.  
  1146.  
  1147. minilax:        $(BINS)
  1148.         $(CC) $(CFLAGS) $(BINS) $(LIB)/libreuse.a -o minilax -lm
  1149.  
  1150. Scanner.rpp Parser.lalr:        minilax.pars
  1151.         cg -cxzj minilax.pars;
  1152.  
  1153. minilax.rex:    minilax.scan Scanner.rpp
  1154.         rpp < minilax.scan > minilax.rex;
  1155.  
  1156. Scanner.h Scanner.c:    minilax.rex
  1157.         rex -cd minilax.rex;
  1158.  
  1159. Parser.h Parser.c:      Parser.lalr
  1160.         lalr -c -d Parser.lalr;
  1161.  
  1162. Tree.h Tree.c:  minilax.cg
  1163.         cg -cdimRDI0 minilax.cg;
  1164.  
  1165. Semantics.h Semantics.c:        minilax.cg
  1166.         cg -cDI0 minilax.cg;
  1167.  
  1168. Definitions.h Definitions.c Definitions.TS:     Definitions.cg
  1169.         cg -cdim4 Definitions.cg;
  1170.  
  1171. Tree.TS:        minilax.cg
  1172.         echo SELECT AbstractSyntax Output | cat - minilax.cg | cg -c4
  1173.  
  1174. Types.h Types.c:        Types.puma Tree.TS
  1175.         puma -cdipk Types.puma;
  1176.  
  1177. ICode.h ICode.c:        ICode.puma Tree.TS Definitions.TS
  1178.         puma -cdi ICode.puma;
  1179.  
  1180. Parser.o:       Parser.h Scanner.h Tree.h Types.h Definitions.h
  1181. Semantics.o:    Semantics.h Tree.h Definitions.h Types.h
  1182. Tree.o:         Tree.h
  1183. Definitions.o:  Definitions.h Tree.h
  1184. Types.o:        Tree.h Types.h
  1185. ICode.o:        Tree.h Types.h ICodeInter.h
  1186. minilax.o:      Scanner.h Parser.h Tree.h Semantics.h Definitions.h ICode.h \
  1187.                 ICodeInter.h Types.o
  1188.  
  1189. lint:   $(SOURCES)
  1190.         lint -I$(HOME)/reuse/c -u *.c
  1191.  
  1192. clean:
  1193.         rm -f Scan*.? Parser.? Tree.? Sema*.? Defi*.? Types.? ICode.? *.TS
  1194.         rm -f core _Debug minilax *Tab mini*.rex Parser.lalr Scan*.rpp yy*.w *.o
  1195.  
  1196. .c.o:
  1197.         $(CC) $(CFLAGS) -c $*.c;
  1198.  
  1199.  
  1200.  
  1201.  
  1202.  
  1203.  
  1204.  
  1205.  
  1206.  
  1207.  
  1208.  
  1209.                                                                             12
  1210.  
  1211.  
  1212. 3.2.  Modula-2
  1213.  
  1214. The first command (target minilax) describes compilation and linking using the
  1215. GMD  Modula-2  compiler  MOCKA. This compiler does its own dependency analysis
  1216. among the sources. Therefore the makefile  does  not  contain  any  dependency
  1217. descriptions  between  sources and binaries.  The -d flag of the compiler call
  1218. mc specifies the directory where the library  reuse is located.  The  rest  of
  1219. the makefile describes the generation phase and the invocation of the tools.
  1220.  
  1221. SOURCES = Scanner.md Scanner.mi Parser.md Parser.mi \
  1222.           Tree.md Tree.mi Semantics.md Semantics.mi \
  1223.           Types.md Types.mi Definitions.md Definitions.mi \
  1224.           ICode.md ICode.mi ICodeInter.md ICodeInter.mi minilax.mi
  1225.  
  1226. minilax:        $(SOURCES)
  1227.         echo p minilax | mc -d ../../reuse/src
  1228.  
  1229. Scanner.rpp Parser.lalr:        minilax.pars
  1230.         cg -xzj minilax.pars;
  1231.  
  1232. minilax.rex:    minilax.scan Scanner.rpp
  1233.         rpp < minilax.scan > minilax.rex;
  1234.  
  1235. Scanner.md Scanner.mi Scanner.Tab:      minilax.rex
  1236.         rex -d minilax.rex;
  1237.  
  1238. Parser.md Parser.mi Parser.Tab: Parser.lalr
  1239.         lalr -d Parser.lalr;
  1240.  
  1241. Tree.md Tree.mi:        minilax.cg
  1242.         cg -dimRDI0 minilax.cg;
  1243.  
  1244. Semantics.md Semantics.mi:      minilax.cg
  1245.         cg -DI0 minilax.cg;
  1246.  
  1247. Definitions.md Definitions.mi Definitions.TS:   Definitions.cg
  1248.         cg -dim4 Definitions.cg;
  1249.  
  1250. Tree.TS:        minilax.cg
  1251.         echo SELECT AbstractSyntax Output | cat - minilax.cg | cg -4
  1252.  
  1253. Types.md Types.mi:      Types.puma Tree.TS
  1254.         puma -dipk Types.puma;
  1255.  
  1256. ICode.md ICode.mi:      ICode.puma Tree.TS Definitions.TS
  1257.         puma -di ICode.puma;
  1258.  
  1259. clean:
  1260.         rm -f Scan*.m? Parser.m? Tree.m? Sema*.m? Defi*.m? Types.m? ICode.m?
  1261.         rm -f core *.TS *.[dimor] _Debug minilax *Tab mini*.rex Parser.lalr Scan*.rpp
  1262.  
  1263.  
  1264.  
  1265.  
  1266.  
  1267.  
  1268.  
  1269.  
  1270.  
  1271.  
  1272.  
  1273.  
  1274.                                                                              1
  1275.  
  1276.  
  1277. Contents
  1278.  
  1279.         Abstract ........................................................    1
  1280. 1.      Document Overview ...............................................    1
  1281. 1.1.    Format ..........................................................    2
  1282. 1.2.    Documents .......................................................    2
  1283. 1.3.    Outlines ........................................................    2
  1284. 2.      Generating a Compiler ...........................................    5
  1285. 2.1.    Scanning and Parsing ............................................    6
  1286. 2.2.    Semantic Analysis and Transformation ............................    7
  1287. 2.3.    Compiling and Linking ...........................................    8
  1288. 3.      Makefile ........................................................    9
  1289. 3.1.    C ...............................................................   10
  1290. 3.2.    Modula-2 ........................................................   12
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326.  
  1327.  
  1328.  
  1329.  
  1330.  
  1331.  
  1332.  
  1333.  
  1334.  
  1335.